package com.czl.module_storehouse.adapter.tree;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Space;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.czl.base.data.bean.tengyun.CategoryBean;
import com.czl.module_storehouse.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author xubin
 * @date on 2019/5/21  18:11
 */
public abstract class ListTreeAdapter<VH extends ListTreeAdapter.ListTreeViewHolder>
        extends RecyclerView.Adapter<VH> {

    protected ListTree tree;
    protected float indentation;

    //展开和收起图标的Drawable资源id
//    private Bitmap expandIcon = null;
//    private Bitmap collapseIcon = null;


    public ListTreeAdapter(ListTree tree) {
        this.tree = tree;
    }

    public ListTreeAdapter(ListTree tree, float indentation) {
        this.tree = tree;
        this.indentation = indentation;
    }

    public ListTreeAdapter(ListTree tree, Bitmap expandIcon, Bitmap collapseIcon) {
        this.tree = tree;
//        this.expandIcon = expandIcon;
//        this.collapseIcon = collapseIcon;
    }

    public ListTree getTree() {
        return tree;
    }

    @NonNull
    @Override
    final public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ViewGroup container = (ViewGroup) inflater.inflate(
                R.layout.row_container_layout, parent, false);

        //响应在Arrow上的点击事件，执行收缩或展开
        ImageView arrowIcon = container.findViewById(R.id.list_tree_arrow_icon);
        //跟据列表控件的宽度为它计算一个合适的大小
        int w = parent.getMeasuredWidth();
        arrowIcon.getLayoutParams().width = w / 15;
        arrowIcon.getLayoutParams().height = w / 15;

        //子类创建自己的row view
        VH vh = onCreateNodeView(container, viewType);

        vh.containerView = container;
        vh.arrowIcon = arrowIcon;
        vh.headSpace = container.findViewById(R.id.list_tree_head_space);
        vh.initView();
        return vh;
    }

    protected abstract @NotNull
    VH onCreateNodeView(ViewGroup parent, int viewType);

    protected abstract void onBindNodeViewHolder(VH viewHolder, int position);

    protected void onBindNodeViewHolder(VH viewHolder, int position, @NonNull List<Object> payloads) {

    }

    @Override
    final public int getItemViewType(int position) {
        int count = 0;
        ListTree.TreeNode node = tree.getNodeByPlaneIndex(position);
        return node.getLayoutResId();
    }

    @Override
    final public void onBindViewHolder(@NonNull VH holder, int position) {
        //get node at the position
        ListTree.TreeNode node = tree.getNodeByPlaneIndex(position);

        //跟据node的层深，改变缩进距离,从0开始计
        int layer = tree.getNodeLayerLevel(node);
        ViewGroup.LayoutParams layoutParams = holder.headSpace.getLayoutParams();
        layoutParams.width = (int) (layer * indentation);
        holder.headSpace.setLayoutParams(layoutParams);
        //给子类机会去绑定行数据
        onBindNodeViewHolder(holder, position);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            onBindNodeViewHolder(holder, position, payloads);
        }
    }

    @Override
    final public int getItemCount() {
        return tree.size();
    }

    public void notifyTreeItemInserted(ListTree.TreeNode parent, ListTree.TreeNode node) {
        int parentPlaneIndex = tree.getNodePlaneIndex(parent);
        if (parent.isExpand() && node != null) {
            //已展开
            super.notifyItemInserted(tree.getNodePlaneIndex(node));
        } else {
            //未展开，需展开爸爸
            int count = tree.expandNode(parentPlaneIndex);
            //通知改变爸爸的状态
            super.notifyItemChanged(parentPlaneIndex);
            //通知更新展开的子孙行们
            notifyItemRangeInserted(parentPlaneIndex + 1, parent.getDescendantCount());
        }
    }

    public void notifyTreeItemRemoved(ListTree.TreeNode node) {
        if (node != null) {
            int nodePlaneIndex = tree.getNodePlaneIndex(node);
            tree.removeNode(node);
            super.notifyItemRemoved(nodePlaneIndex);
            super.notifyItemRangeChanged(nodePlaneIndex, tree.size() - nodePlaneIndex);
        }
    }

    public class ListTreeViewHolder extends RecyclerView.ViewHolder {
        protected ViewGroup containerView;
        protected ImageView arrowIcon;
        protected Space headSpace;

        public ListTreeViewHolder(View itemView) {
            super(itemView);
        }

        protected void initView() {
            arrowIcon.setOnClickListener(v -> {
                int planePos = getAbsoluteAdapterPosition();
                ListTree.TreeNode node = tree.getNodeByPlaneIndex(planePos);
                if (node.isShowExpandIcon()) {
                    int nodePlaneIndex = tree.getNodePlaneIndex(node);
                    if (node.isExpand()) {
                        //收起
                        int count = tree.collapseNode(nodePlaneIndex);
                        notifyItemChanged(nodePlaneIndex);
                        //通知view将相关的行删掉
                        notifyItemRangeRemoved(nodePlaneIndex + 1, count);
                    } else {
                        //展开
                        int count = tree.expandNode(nodePlaneIndex);
                        notifyItemChanged(nodePlaneIndex);
                        //通知view插入相关的行
                        notifyItemRangeInserted(nodePlaneIndex + 1, count);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener<T> {
        /**
         * 点击
         *
         * @param t
         * @param title
         * @param pIdPath
         */
        void onItemClick(T t, String title, String pIdPath);
    }
}
